package com.openvalley.cyj.migration;

import com.openvalley.cyj.migration.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.IAbilityContinuation;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.bundle.IBundleManager;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.security.SystemPermission;

public class MainAbility extends Ability implements IAbilityContinuation {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0x00001, MainAbility.class.getSimpleName());

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());

        verifyAndRequestSelfPermission();
    }

    /**
     * 在此处验证和请求用户权限
     */
    private void verifyAndRequestSelfPermission() {
        try{
            //返回值0有，-1没有
            if(verifyCallingOrSelfPermission(SystemPermission.DISTRIBUTED_DATASYNC) == IBundleManager.PERMISSION_GRANTED){
//            if(verifySelfPermission(SystemPermission.LOCATION) == IBundleManager.PERMISSION_GRANTED){
                //如果有权限
            }else {
                //如果没授权就看这个权限能否动态授权
                if(canRequestPermission(SystemPermission.DISTRIBUTED_DATASYNC)){
                    //如果可以弹窗授权
                    requestPermissionsFromUser(new String[] {SystemPermission.DISTRIBUTED_DATASYNC}, 0x10001);
                }else {
                    //如果不能动态授权
                    return ;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 授权的回调
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
        if(requestCode == 0x10001){
            if(grantResults[0] == IBundleManager.PERMISSION_GRANTED){
                //如果授权成功（0）
                HiLog.info(LABEL_LOG, permissions[0] + "权限授权成功。");
            }else {
                //如果没有授权成功（-1）
                return ;
            }
        }
    }

    /**
     * 准备开始迁移的回调
     * @return
     */
    @Override
    public boolean onStartContinuation() {
        //返回值为true才发起迁移
        HiLog.info(LABEL_LOG, "MainAbility onStartContinuation");
        return true;
    }

    /**
     * 当保存数据（要迁移的数据）的回调
     * @param intentParams
     * @return
     */
    @Override
    public boolean onSaveData(IntentParams intentParams) {
        HiLog.info(LABEL_LOG, "MainAbility onSaveData");
        return true;
    }

    /**
     * 当恢复数据的回调（应该是被迁移的设备调用
     * @param intentParams
     * @return
     */
    @Override
    public boolean onRestoreData(IntentParams intentParams) {
        //返回为true代表恢复数据成功
        HiLog.info(LABEL_LOG, "MainAbility onRestoreData");
        return true;
    }

    /**
     * 迁移完成的回调
     * @param i
     */
    @Override
    public void onCompleteContinuation(int i) {
        HiLog.info(LABEL_LOG, " MainAbility onCompleteContinuation");
    }

    /**
     * 迁移过程中失败的回调
     */
    @Override
    public void onRemoteTerminated() {
        IAbilityContinuation.super.onRemoteTerminated();
        HiLog.info(LABEL_LOG, "MainAbility onRemoteTerminated");
    }

    /**
     * 回迁允许的情况下，远端中断
     * @param errorCode
     */
    @Override
    public void onFailedContinuation(int errorCode) {
        IAbilityContinuation.super.onFailedContinuation(errorCode);
        HiLog.info(LABEL_LOG, "MainAbility onFailedContinuation");
    }
}
